﻿#include "speechmodulethread.h"
#include "mainwindow.h"

extern QString comtest;
extern int speflv;
extern intListener doorStatus;

speechModuleThread::speechModuleThread(QObject *parent): QObject(parent)
{
    tcpclient = TcpClient::getInstance();


    labelTimer = new QTimer(this);
    labelTimer->stop();    // 默认定时器关闭
    labelTimer->setSingleShot(false);
    labelTimer->setTimerType(Qt::PreciseTimer);
    labelTimer->setInterval(5000);
    connect(labelTimer, &QTimer::timeout, this, &speechModuleThread::closeMovieLabel);
    this->moveToThread(&thread);
    thread.start();
}

void speechModuleThread::serialinfo()
{
    QString serialNamePort=comtest;
    serialPort = new QSerialPort(this);
    serialPort->close();
    QSerialPort::BaudRate baudRate;
    baudRate = QSerialPort::Baud115200;
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);
    serialPort->setPortName(serialNamePort);
    serialPort->setBaudRate(baudRate);
    if(serialPort->open(QIODevice::ReadWrite)){
        qDebug()<<"success";
    }else{
        qDebug()<<"false";
    }
    qDebug()<<serialPort->isOpen();
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortRead_Slot()));// 串口读取
}

void speechModuleThread::stop()
{
    qDebug()<<serialPort->isOpen();
    if (serialPort->isOpen()) {
        serialPort->close(); // 关闭串口
    }
}

void speechModuleThread::run()
{
    if(speflv==1){
        if(MainWindow::speechflagkg.get()==0){
            serialinfo();
        }else if(MainWindow::speechflagkg.get()==1){
            stop();
        }
    }
}

// 接收消息
void speechModuleThread::serialPortRead_Slot()
{
    QByteArray buffer = serialPort->readAll();
    QString ss;
    for(int c :buffer)
    {
        if(c>=0)
        {
            ss += QString(" %1")
                    .arg(c, 2, 16, QChar('0'));
        }
        else
        {
            ss += QString(" %1")
                    .arg(c+256, 2, 16, QChar('0'));
        }
    }
    QString Data=ss.remove(QRegExp("\\s"));
    int syfl = MainWindow::experimentStatus.get();
    qDebug()<<Data;
    if(Data=="0001"){
        if((syfl==0||syfl==2)&&doorStatus.get()==0){
            doorStatus.set(1);
        }else{

        }
    }else if(Data=="0002"){
        if(doorStatus.get()==1){
            doorStatus.set(0);
        }else{

        }
    }else if(Data=="0003"){
        qDebug()<<"启动实验";
    }else if(Data=="0004"){
        if(syfl==1){
            QString senddata = "6200003A000A000000010004459E55AA";
            QByteArray all;
            //分析字符串格式中是否带空格
            for(int i = 0;i<senddata.size();i++)
            {
                if(senddata[i]==' ')
                    continue;   //跳过

                int num  = senddata.mid(i,2).toUInt(nullptr,16);
                i++;
                all.append(num);
            }
            tcpclient->ClientSendingData(all);
            MainWindow::experimentStatus.set(2);
            qDebug()<<"停止实验";
        }else{

        }
    }else if(Data=="0005"){
        qDebug()<<"关机";
    }else if(Data=="0006"){
        if((syfl==0||syfl==2)&&doorStatus.get()==0){
            qDebug()<<"复位";
        }
    }
}

void speechModuleThread::closeMovieLabel()
{
    label->close();
    labelTimer->stop();
    movieDoor->stop();
    delete movieDoor;
    movieDoor=0;
}

void speechModuleThread::serialclo(){
    serialPort->close();
}
